﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DispatchAssist.Model;
using DispatchAssist.Service;
using CarolLib;
using DispatchAssist.BusinessLogical.Model;
using CarolLib.Control;
using System.Text.RegularExpressions;

namespace DispatchAssist.AppHost
{
    public partial class frmManualSendAcrarsAlert : Form
    {
        #region Members

        NormalAlertInfo alertinfo = new NormalAlertInfo();
        AlertMessageGetterService service = new AlertMessageGetterService();
        Sunisoft.IrisSkin.SkinEngine skinEngine1;
        MeteoMsgAlert mmalertF = new MeteoMsgAlert();
        delegate void FunDelegate();

        private readonly object locker = new object();

        #endregion

        #region Ctor.

        public frmManualSendAcrarsAlert()
        {
            InitializeComponent();
            skinEngine1 = SkinConfig.Instance.EnsureSkin(this);
        }

        public frmManualSendAcrarsAlert(NormalAlertInfo info, MeteoMsgAlert mmalert)
            : this()
        {
            alertinfo = info;
            mmalertF = mmalert;
        }

        #endregion

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (richtxtACARSContent.Text == string.Empty)
            {
                MessageBox.Show("报文内容不能为空！");
                richtxtACARSContent.Focus();
                return;
            }
            if (!IsNotIncludedChinese(richtxtACARSContent.Text))
            {
                MessageBox.Show("报文内容不能有汉字！");
                richtxtACARSContent.Focus();
                return;
            }

            service.InesetAcarsMsgModelAsync(alertinfo.AlertID.ToString(), alertinfo.TailNo, richtxtACARSContent.Text, service.DASoapHeaderValue.UserId, alertinfo.AlertID.ToString());
            //SysGlobal.oldNormalAlertInfoList.Remove(alertinfo);
            //AlertContext.Dic_Acars.Remove(alertinfo.AlertID.ToString());
            //this.Close();
            this.Close();
        }
        public void wjs()
        {
            FunDelegate funDelegate = new FunDelegate(BindBaseInfoOfAlertMessage);

            /*========================================================
             * 使用this.BeginInvoke方法
             * （也可以使用this.Invoke()方法）
            ========================================================*/

            // this.BeginInvoke(被调用的方法的委托，要传递的参数[Object数组])


            IAsyncResult aResult = funDelegate.BeginInvoke(callbackm, this);
        }

        private void callbackm(IAsyncResult result)
        {
            if (result.IsCompleted)
            {
                //dddd.Invoke(result.AsyncState);
                AlertContext.Dic_Acars_List.Add(((frmManualSendAcrarsAlert)result.AsyncState));

                //((frmPromptAlert)result.AsyncState).Show();
            }

        }

        private void frmManualSendAcrarsAlert_Load(object sender, EventArgs e)
        {
            service.DASoapHeaderValue = AuthContext.Current.GetSoapHeader();
            BindBaseInfoOfAlertMessage();

            service.InesetAcarsMsgModelCompleted += new InesetAcarsMsgModelCompletedEventHandler(service_InesetAcarsMsgModelCompleted);
            service.UpdateAlertStatusCompleted += new UpdateAlertStatusCompletedEventHandler(service_UpdateAlertStatusCompleted);
        }

        void service_InesetAcarsMsgModelCompleted(object sender, AsyncCompletedEventArgs e)
        {
            //lock (locker)
            {
                AlertContext.Dic_Acars.Remove((string)e.UserState);
                AlertContext.Dic_Dispose_Acars.Remove((string)e.UserState);
                AlertContext.CountI++;
            }
        }

        void service_UpdateAlertStatusCompleted(object sender, AsyncCompletedEventArgs e)
        {
            //lock (locker)
            if (e.Error == null)
            {
                AlertContext.Dic_Acars.Remove((string)e.UserState);
                AlertContext.Dic_Dispose_Acars.Remove((string)e.UserState);
                AlertContext.CountI++;
            }
            else
            {
                AlertContext.Dic_Acars.Remove((string)e.UserState);
                AlertContext.Dic_Dispose_Acars.Remove((string)e.UserState);
            }
        }

        private void BindBaseInfoOfAlertMessage()
        {
            txtFlightNo.Text = alertinfo.Carrier + alertinfo.FlightNo;
            txtTailNo.Text = alertinfo.TailNo;
            txtAirPort.Text = alertinfo.AirportFullName;
            txtAlertLevel.Text = alertinfo.WarnLevelName;
            txtAlertTime.Text = alertinfo.WarnTime.ToString();
            richtxtACARSContent.Text = alertinfo.MessageBody;
        }

        private void btnNoSend_Click(object sender, EventArgs e)
        {
            string[] ids = { alertinfo.AlertID.ToString() };
            ////service.UpdateAlertStatus(ids, (int)Enums.AlertActionFlag.ManualSendAcrarsAlert, service.DASoapHeaderValue.UserId);
            //SysGlobal.oldNormalAlertInfoList.Remove(alertinfo);
            ////AlertContext.Dic_Acars.Remove(alertinfo.AlertID.ToString());
            AlertContext.Dic_Dispose_Acars.Add(alertinfo.AlertID.ToString(), this);
            service.UpdateAlertStatusAsync(ids, (int)Enums.AlertActionFlag.ManualSendAcrarsAlert, service.DASoapHeaderValue.UserId, alertinfo.AlertID.ToString());
            this.Close();
        }

        public static bool IsValidateOfEnglish(string str)
        {
            bool flag = false;
            if (!Regex.IsMatch(str, @"^[A-Za-z0-9 ]+$"))
            {
                flag = false;
            }
            else
            {
                flag = true;
            }
            return flag;
        }

        public static bool IsNotIncludedChinese(string str)
        {
            bool flag = false;
            if (!Regex.IsMatch(str, @"^[^\u4e00-\u9fa5]{0,}$"))
            {
                flag = false;
            }
            else
            {
                flag = true;
            }
            return flag;
        }



    }
}
